package com.leyou.controller;

import com.leyou.common.vo.PageResult;
import com.leyou.dto.GoodsDTO;
import com.leyou.dto.SearchRequest;
import com.leyou.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author: guojy
 * @date: 2020/3/8 15:33
 * @Description: 搜索微服务
 * @version: 1.0
 */
@RestController
public class SearchController {
    @Autowired
    private SearchService searchService;

    //api.leyou.com/api/search/page
    // TODO ES实现分页搜索
    @PostMapping("page")
    public ResponseEntity<PageResult<GoodsDTO>> search(@RequestBody SearchRequest searchRequest) {
        PageResult<GoodsDTO> goodsList = searchService.search(searchRequest);
        return ResponseEntity.ok(goodsList);
    }

    /**
     * 查询品牌和分类信息，用于构建  前端过滤条件
     * @param searchRequest
     * @return
     */
    @PostMapping("filter")
    public ResponseEntity<Map<String, List<?>>> filter(@RequestBody SearchRequest searchRequest) {
        Map<String, List<?>> map = searchService.filter(searchRequest);
        return ResponseEntity.ok(map);
    }
}
